﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Departmentss;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Bedroomss
{
    public class AddBedroomssHander : IRequestHandler<AddBedroomssComand, Result>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 企业仓储
        /// </summary>
        private readonly IBaseRepository<Bedrooms> bedroomsrepository;

        public AddBedroomssHander(IMapper mapper, IBaseRepository<Bedrooms> bedroomsrepository)
        {
            this.mapper = mapper;
            this.bedroomsrepository = bedroomsrepository;
        }
        /// <summary>
        /// 栋舍管理添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(AddBedroomssComand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            Bedrooms entity = mapper.Map<Bedrooms>(request);
            var res = await bedroomsrepository.Add(entity);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
            }
            result.Msg = "添加成功";
            return result;
        }
    }
}
